package GUI.TabPanels.AdminTabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.IntegerComparator;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class AddRoomPanel extends JPanel{
	private JButton add_room_button;
	private JButton refreshButton;

	private JLabel room_number_label;
	private JTextField room_number_text;

	private JLabel room_type_label;
	private JComboBox room_type_text;

	private JLabel hotel_label;
	private JComboBox hotel_text;

	private JTable roomTable;

	private JPanel topPanel;
	private JPanel middlePanel;
	private JScrollPane scrollPane;

	private String[] columnNames =
	{
			"Room ID",
			"Room #",
			"Room Type",
			"Hotel"
	};

	public AddRoomPanel(){
		this.setBackground(Color.PINK);
		this.setLayout(new BorderLayout());
		this.SetUpTopPanel();
		this.SetUpMiddlePanel();
		this.SetUpBottomPanel();
	}

	public void SetUpTopPanel(){
		topPanel = new JPanel();
		this.add(topPanel, BorderLayout.NORTH);
		topPanel.setLayout(new GridLayout(0, 2, -300, 7));
		topPanel.setBackground(Color.pink);

		room_number_label = new JLabel("Room #:");
		room_number_text = new JTextField(9);
		room_number_text.setEditable(true);
		topPanel.add(room_number_label);
		topPanel.add(room_number_text);

		room_type_label = new JLabel("Room Type:");
		String[] room_types = Controller.GetRoomTypes();
		room_type_text = new JComboBox(room_types);
		try{
			room_type_text.setSelectedIndex(0);
		}catch(IllegalArgumentException e){
			
		}
		topPanel.add(room_type_label);
		topPanel.add(room_type_text);

		hotel_label = new JLabel("Hotel:");
		String[] hotels = Controller.GetHotels();
		hotel_text = new JComboBox(hotels);
		try{
			hotel_text.setSelectedIndex(0);
		}catch(IllegalArgumentException e){
		}
		topPanel.add(hotel_label);
		topPanel.add(hotel_text);
	}

	public void SetUpMiddlePanel(){
		JLabel emptyLabel = new JLabel();
		middlePanel = new JPanel();
		middlePanel.setBackground(Color.pink);

		add_room_button = new JButton("Add Room");
		add_room_button.addActionListener(new AddRoomButton());

		refreshButton = new JButton("Refresh Lists");
		refreshButton.addActionListener(new RoomTypeListener());
		refreshButton.addActionListener(new HotelsListener());

		middlePanel.add(emptyLabel);
		middlePanel.add(add_room_button);
		middlePanel.add(refreshButton);
		middlePanel.add(emptyLabel);

		this.add(middlePanel, BorderLayout.CENTER);
	}

	public void SetUpBottomPanel(){
		Object[][] tableData = Controller.GetRooms();

		CreateTable(tableData);
	}

	private void Update()
	{

		Object[][] tableData = Controller.GetRooms();

		CreateTable(tableData);

		roomTable.updateUI();
		
		this.setVisible(false);
		this.setVisible(true);
	}

	private void CreateTable(Object[][] tableData){
		roomTable = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false;
			} 
		};
		roomTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());
		sorter.setComparator(1, new IntegerComparator());

		roomTable.setRowSorter(sorter);
		roomTable.setAutoCreateRowSorter(false);

		roomTable.setPreferredScrollableViewportSize(new Dimension(500, 300));
		roomTable.setFillsViewportHeight(true);

		if(scrollPane!=null)
			remove(scrollPane);
		scrollPane = new JScrollPane(roomTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	private class AddRoomButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try
			{
				int room_number = Integer.parseInt(room_number_text.getText());

				String[] data = {
						String.valueOf(Math.max(Integer.parseInt(room_number_text.getText()), 1)),
						(String)room_type_text.getSelectedItem(),
						(String)hotel_text.getSelectedItem()						
				};
				System.out.println("adding room '" + room_number + "' type: '" + room_type_text.getSelectedItem()+"'");

				Controller.AddRoom(data);
				JOptionPane.showMessageDialog(null, "Room Created!");
			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Room Number must be an integer.");
			}
			Update();			
		}
	}

	private class RoomTypeListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String[] roomTypes = Controller.GetRoomTypes();
			room_type_text.setModel(new DefaultComboBoxModel(roomTypes));
		}
	}

	private class HotelsListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String[] hotels = Controller.GetHotels();
			hotel_text.setModel(new DefaultComboBoxModel(hotels));
		}
	}
}
